﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using TecentCOS.ViewModels;
using System.Linq;

namespace TecentCOS.Extensions
{
    public static class MvcHtmlExtension
    {
        public static MvcHtmlString MenuLink(this HtmlHelper helper, string text, string action, string controller, string innertHtml = "")
        {
            var routeData = helper.ViewContext.RouteData.Values;
            var currentController = routeData["controller"];
            var currentAction = routeData["action"];
            string htmlString = "";
            if (String.Equals(action, currentAction as string, StringComparison.OrdinalIgnoreCase) &&
                String.Equals(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
            {
                htmlString = $@"<a class='current' href='/{controller}/{action}'>{innertHtml}{text}</a>";
            }
            else
            {
                htmlString = $@"<a class='' href='/{controller}/{action}'>{innertHtml}{text}</a>";
            }
            return new MvcHtmlString(htmlString);
            //return helper.ActionLink(text, action, controller);
        }
        //int level, string action, string controller, string icon = ""
        public static MvcHtmlString CustomMenu(this HtmlHelper helper, string currentPath)
        {
            List<TreeMenuModel> menus = GetMenuDataSource();


            StringBuilder sb = new StringBuilder();
            //设置active状态
            foreach (var menu in menus)
            {
                bool active = currentPath == menu.Path;
                menu.isActive = active;
                if (menu.isActive&&menu.ParentId != Guid.Empty) ExpandParent(menu, menus);
            }
           
            var data = menus.Where<TreeMenuModel>(n => n.ParentId == Guid.Empty).ToList();
            foreach (var menu in data)
            {
                
                
                AddChildMenu(menu, menus);
                
             

            }
    
         
            sb = BuildHtml(data, sb);


            return new MvcHtmlString(sb.ToString());
        }

        public static void AddChildMenu(TreeMenuModel current, List<TreeMenuModel> source)
        {
            //查出所有儿子
            var children = source.Where<TreeMenuModel>(n => n.ParentId == current.Id);
            if (children.Any())
            {
                
                //遍历所有子节点
                foreach (var child in children)
                {
                    current.Children.Add(child);
                    AddChildMenu(child, source);
                }
            }
        }
        private static void ExpandParent(TreeMenuModel current, List<TreeMenuModel> source)
        {
            string name=current.Name;
            var parents = source.Where<TreeMenuModel>(n => n.Id == current.ParentId);
            if (!parents.Any()) return;
            var parent = parents.First();
            parent.isActive = current.isActive;

            if (parent.ParentId != Guid.Empty)
            {
                ExpandParent(parent, source);
            }
        }
        public static StringBuilder BuildHtml(List<TreeMenuModel> menus, StringBuilder resultString)
        {
            foreach (var menu in menus)
            {
                if (menu.Children.Count == 0)
                {
                    string navString = $@"<li class='{(menu.isActive ? "active" : "")}'><a href='{menu.Path}'><i class='menu-icon fa {menu.Icon}'></i><span class='menu-text'>{menu.Title}</span></a><b class='arrow'></b></li>";
                    resultString.Append(navString);
                }
                else
                {
               
                    resultString.Append( $@"<li class='{(menu.isActive ? "open" : "")}'><a href='{menu.Path}' class='dropdown-toggle'><i class='menu-icon fa {menu.Icon}'></i><span class='menu-text'>{menu.Title}</span><b class='arrow fa fa-angle-down'></b></a><b class='arrow'></b>");
                    resultString.Append("<ul class='submenu'>");
                    BuildHtml(menu.Children, resultString);
                    resultString.Append("</ul>");
                    resultString.Append("</li>");
                }

            }
            return resultString;

        }
        private static List<TreeMenuModel> GetMenuTreeDataSource()
        {


            List<TreeMenuModel> ds = new List<TreeMenuModel>();
            var index = new TreeMenuModel();
            index.Id = Guid.NewGuid();
            index.Name = "Index";
            index.Title = "首页";
            index.Path = "/";
            index.Icon = "fa-tachometer";
            ds.Add(index);
            //二级菜单
            var blindwatermark = new TreeMenuModel();
            blindwatermark.Id = Guid.NewGuid();
            blindwatermark.Name = "blindWaterMark";
            blindwatermark.Title = "盲水印";
            blindwatermark.Path = "";//二级菜单
            blindwatermark.Icon = "fa-picture-o";
            blindwatermark.Children = new List<TreeMenuModel>();
            ds.Add(blindwatermark);

            var addWaterMark = new TreeMenuModel();
            addWaterMark.Id = Guid.NewGuid();
            addWaterMark.Name = "addWaterMark";
            addWaterMark.Path = "/watermark/add";
            addWaterMark.Title = "添加盲水印";
            addWaterMark.ParentId = blindwatermark.Id;
            addWaterMark.Icon = "fa-caret-right";
            blindwatermark.Children.Add(addWaterMark);

            ds.Add(addWaterMark);
            var extractWaterMark = new TreeMenuModel();
            extractWaterMark.Id = Guid.NewGuid();
            extractWaterMark.Name = "extractWaterMark";
            extractWaterMark.Path = "/watermark/extract";
            extractWaterMark.Title = "提取盲水印";
            extractWaterMark.Icon = "fa-caret-right";
            extractWaterMark.ParentId = blindwatermark.Id;
            blindwatermark.Children.Add(extractWaterMark);
            ds.Add(blindwatermark);
            //三级菜单

            var threeLevelMenu = new TreeMenuModel();
            threeLevelMenu.Id = Guid.NewGuid();
            threeLevelMenu.Name = "blindWaterMark";
            threeLevelMenu.Title = "三级菜单";
            threeLevelMenu.Path = "";//
            threeLevelMenu.Icon = "fa-picture-o";
            threeLevelMenu.Children = new List<TreeMenuModel>();
            ds.Add(threeLevelMenu);

            var threeLevelMenu01 = new TreeMenuModel();
            threeLevelMenu01.Id = Guid.NewGuid();
            threeLevelMenu01.Name = "blindWaterMark";
            threeLevelMenu01.Title = "三级菜单-01";
            threeLevelMenu01.Path = "";//
            threeLevelMenu01.ParentId = threeLevelMenu.Id;
            ds.Add(threeLevelMenu01);
            threeLevelMenu01.Children = new List<TreeMenuModel>();

            var threeLevel0101 = new TreeMenuModel()
            {
                Title = "三级菜单-01-01",
                Path = "/Home/Contact",
                ParentId = threeLevelMenu01.Id
            };
            threeLevelMenu01.Children.Add(threeLevel0101);
            ds.Add(threeLevel0101);

            var threeLevel0102 = new TreeMenuModel()
            {
                Title = "三级菜单-01-02",
                Path = "/Home/About",
                ParentId = threeLevelMenu01.Id
            };

            threeLevelMenu01.Children.Add(threeLevel0102);

            ds.Add(threeLevel0102);
            var threeLevelMenu02 = new TreeMenuModel();
            threeLevelMenu02.Id = Guid.NewGuid();
            threeLevelMenu02.Name = "blindWaterMark";
            threeLevelMenu02.Title = "三级菜单-02";
            threeLevelMenu02.Path = "";//
            threeLevelMenu02.ParentId = threeLevelMenu.Id;
            threeLevelMenu02.Children = new List<TreeMenuModel>();


            threeLevelMenu02.Children.Add(new TreeMenuModel()
            {
                Title = "三级菜单-02-01",
                Path = "/Home/Contact1",
                ParentId = threeLevelMenu02.Id
            });

            threeLevelMenu02.Children.Add(new TreeMenuModel()
            {
                Title = "三级菜单-02-02",
                Path = "/Home/About2",
                ParentId = threeLevelMenu02.Id
            });

            threeLevelMenu.Children.Add(threeLevelMenu01);
            threeLevelMenu.Children.Add(threeLevelMenu02);

            ds.AddRange(threeLevelMenu02.Children);
            return ds;

        }


        private static List<TreeMenuModel> GetMenuDataSource()
        {
            List<TreeMenuModel> ds = new List<TreeMenuModel>();

            var menu1 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "Index",
                Title = "首页",
                Path = "/",
                Icon = "fa-tachometer",
                ParentId = Guid.Empty
            };
            var menu2 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "blindWaterMark",
                Title = "盲水印",
                Path = "",
                Icon = "fa-picture-o",
                ParentId = Guid.Empty
            };
            var menu2_1 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "添加盲水印",
                Title = "添加盲水印",
                Path = "/watermark/add",
                Icon = "",
                ParentId = menu2.Id
            };
            var menu2_2 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "提取盲水印",
                Title = "提取盲水印",
                Path = "/watermark/extract",
                Icon = "",
                ParentId = menu2.Id
            };
            var menu3 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单",
                Title = "测试三级菜单",
                Path = "",
                Icon = "fa-tachometer",
                ParentId = Guid.Empty
            };
            var menu3_1 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单01",
                Title = "测试三级菜单01",
                Path = "",
                Icon = "",
                ParentId = menu3.Id
            };
            var menu3_2 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单02",
                Title = "测试三级菜单02",
                Path = "",
                Icon = "",
                ParentId = menu3.Id
            };
            var menu3_1_1 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单01-01",
                Title = "测试三级菜单01-01",
                Path = "/Home/Contact",
                Icon = "",
                ParentId = menu3_1.Id
            };
            var menu3_1_2 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单01-02",
                Title = "测试三级菜单01-02",
                Path = "/Home/Contact",
                Icon = "",
                ParentId = menu3_1.Id
            };

            var menu3_2_1 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单02-01",
                Title = "测试三级菜单02-01",
                Path = "/Home/About213",
                Icon = "",
                ParentId = menu3_2.Id
            };

            var menu3_2_2 = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "测试三级菜单02 - 02",
                Title = "测试三级菜单02-01",
                Path = "/Home/About12321",
                Icon = "",
                ParentId = menu3_2.Id
            };

            var about = new TreeMenuModel()
            {
                Id = Guid.NewGuid(),
                Name = "介绍",
                Title = "介绍",
                Path = "/Home/About",
                Icon = "fa-tag",
                ParentId = Guid.Empty
            };
           
            var array = new TreeMenuModel[] { menu1, menu2, menu2_1, menu2_2, menu3, menu3_1, menu3_2, menu3_1_1, menu3_1_2, menu3_2_1, menu3_2_2 };
            ds.AddRange(array);
            ds.Add(about);
            return ds;
        }

    }
}